package msu.ml.data;

import java.io.*;
import msu.ml.core.*;

/**
 * IDataLoader derivative that can load ARFF files.
 *
 * @author Reginald M Mead
 * @version 1.0
 */
public class ArffDataLoader implements IDataLoader 
{

	/**
	 * Returns true if the specified file is an ARFF file
	 *
	 * @param target file to load
	 * @return true if file is an ARFF file
	 */
	public boolean isSupportedData(String target)
	{
		return target.endsWith(".arff");
	}

	/**
	 * Loads an ARFF file into an NxInstances object
	 * 
	 * @param target file to load
	 * @return NxInstances loaded from the specified file
	 */
	public NxInstances load(String target)
   {
      try
      {
         NxInstances instances = new NxInstances(
            new BufferedReader(new FileReader(target)));
         instances.setSourceFile(target);
         instances.setClassIndex(instances.numAttributes() - 1);
         return instances;
      }
      catch(Exception e)
      {
         return null;
      }
   }

}
